package cn.xiaomubiao.oschine.fragment.Event_fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xiaomubiao.oschine.MyApplication;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.activity.MyInfoBaseActivity;
import cn.xiaomubiao.oschine.adapter.FriendRecylerAdapter;
import cn.xiaomubiao.oschine.adapter.tweets_pager_adapter.TweetsBaseRecyclerAdapter;
import cn.xiaomubiao.oschine.base.BaseEachFragment;
import cn.xiaomubiao.oschine.base.BaseRecyclerViewAdapter;
import cn.xiaomubiao.oschine.bean.Active;
import cn.xiaomubiao.oschine.bean.ActiveList;
import cn.xiaomubiao.oschine.bean.Tweet;
import cn.xiaomubiao.oschine.bean.TweetsList;
import cn.xiaomubiao.oschine.contants.Host;
import cn.xiaomubiao.oschine.util.HttpUtil;
import cn.xiaomubiao.oschine.viewpagerfragment.tweetspagerfragment.TweetsDetailFragment;

/**
 * Created by hasee on 2017/4/8.
 */

public class FrientFragment extends BaseEachFragment {

    @BindView(R.id.recyler_pager)
    XRecyclerView mRecylerPager;
    private int mLoadingNum;
    private List<Active> mActives;
    private View mView;
    private FriendRecylerAdapter mFriendRecylerAdapter;

    @Override
    public View loadSuccess() {
        mView = View.inflate(getActivity(), R.layout.view_tweets_pager_layout, null);
        ButterKnife.bind(this,mView);
        initRecylerView();
        return mView;
    }

    private void initRecylerView() {
        mFriendRecylerAdapter = new FriendRecylerAdapter(mActives);
        mFriendRecylerAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnRecycleViewItemClickListener() {
            @Override
            public void onClick( View v, int position ) {
                position -= 1;
                if(mActives.get(position).getObjectType() == 100) {
                    String url = Host.HOST_DEFAULT+"detail/tweet_detail/"+mActives.get(position).getObjectId()+".xml";
                    Intent intent = new Intent(getActivity(), MyInfoBaseActivity.class);
                    intent.putExtra("id",mActives.get(position).getObjectId());
                    intent.putExtra("title","动弹详情");
                    intent.putExtra("className",TweetsDetailFragment.class.getName());
                    startActivity(intent);
                }



            }
        });

        mRecylerPager.setAdapter(mFriendRecylerAdapter);
        mRecylerPager.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecylerPager.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(getContext(), "刷新", Toast.LENGTH_SHORT).show();
                MyApplication.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mRecylerPager.refreshComplete();
                    }
                },2000);
            }

            @Override
            public void onLoadMore() {
                ++mLoadingNum;
                if(mLoadingNum > 3) {
                    mRecylerPager.loadMoreComplete();
                    mRecylerPager.setNoMore(true);
                    return;
                }
                final String loadingValue = "page" + mLoadingNum + ".xml";
                HttpUtil<ActiveList> httpUtil = new HttpUtil<>(getContext(), ActiveList.class);
                httpUtil.getStringData(Host.HOST_DEFAULT + "list/active_list1/page" + mLoadingNum + ".xml", new HttpUtil.OnGetDataListener<ActiveList>() {
                    @Override
                    public void onSuccess( ActiveList activeList ) {
                        if(activeList  == null) {
                            loadMoreCompleteAndNotifyData();
                            return;
                        }
                        List<Active> actives = activeList.getActivelist();
                        mActives.addAll(actives);
                        loadMoreCompleteAndNotifyData();
                    }

                    @Override
                    public void onFailure( Exception e ) {
                        mCommonPager.showFailPager();
                    }
                });

            }
        });
    }

    private void loadMoreCompleteAndNotifyData() {
        MyApplication.mHandler.post(new Runnable() {
            @Override
            public void run() {
                mRecylerPager.loadMoreComplete();
                mFriendRecylerAdapter.notifyDataSetChanged();
            }
        });
    }


    @Override
    public void loadData() {
        //list/active_list1/page0.xml 测试地址
        mLoadingNum = 0;
        HttpUtil<ActiveList> httpUtil = new HttpUtil<>(getContext(), ActiveList.class);
        httpUtil.getStringData(Host.HOST_DEFAULT + "list/active_list1/page" + mLoadingNum + ".xml", new HttpUtil.OnGetDataListener<ActiveList>() {

            @Override
            public void onSuccess( ActiveList activeList ) {
                mActives = activeList.getActivelist();
                mCommonPager.showSuccessPager(mActives);
            }

            @Override
            public void onFailure( Exception e ) {
                mCommonPager.showFailPager();
            }
        });
    }


}

